package cn.jolywood.cloud.module.crm.controller.admin.obtainguest;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.jolywood.cloud.framework.common.pojo.PageParam;
import cn.jolywood.cloud.framework.common.pojo.PageResult;
import cn.jolywood.cloud.framework.common.pojo.CommonResult;
import cn.jolywood.cloud.framework.common.util.object.BeanUtils;
import static cn.jolywood.cloud.framework.common.pojo.CommonResult.success;

import cn.jolywood.cloud.framework.excel.core.util.ExcelUtils;

import cn.jolywood.cloud.framework.apilog.core.annotation.ApiAccessLog;
import static cn.jolywood.cloud.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.jolywood.cloud.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

import cn.jolywood.cloud.module.crm.controller.admin.obtainguest.vo.*;
import cn.jolywood.cloud.module.crm.service.obtainguest.ObtainGuestService;

@Tag(name = "管理后台 - 订单获客主")
@RestController
@RequestMapping("/crm/obtain-guest")
@Validated
public class ObtainGuestController {

    @Resource
    private ObtainGuestService obtainGuestService;

    @PostMapping("/create")
    @Operation(summary = "创建订单获客主")
    @PreAuthorize("@ss.hasPermission('crm:obtain-guest:create')")
    public CommonResult<Integer> createObtainGuest(@Valid @RequestBody ObtainGuestSaveReqVO createReqVO) {
        return success(obtainGuestService.createObtainGuest(createReqVO, getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新订单获客主")
    @PreAuthorize("@ss.hasPermission('crm:obtain-guest:update')")
    public CommonResult<Boolean> updateObtainGuest(@Valid @RequestBody ObtainGuestSaveReqVO updateReqVO) {
        obtainGuestService.updateObtainGuest(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单获客主")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:obtain-guest:delete')")
    public CommonResult<Boolean> deleteObtainGuest(@RequestParam("id") Integer id) {
        obtainGuestService.deleteObtainGuest(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除订单获客主")
                @PreAuthorize("@ss.hasPermission('crm:obtain-guest:delete')")
    public CommonResult<Boolean> deleteObtainGuestList(@RequestParam("ids") List<Integer> ids) {
        obtainGuestService.deleteObtainGuestListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得订单获客主")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:obtain-guest:query')")
    public CommonResult<ObtainGuestRespVO> getObtainGuest(@RequestParam("id") Integer id) {
        ObtainGuestRespVO obtainGuest = obtainGuestService.getObtainGuest(id);
        return success(obtainGuest);
    }

    @GetMapping("/page")
    @Operation(summary = "获得订单获客主分页")
    @PreAuthorize("@ss.hasPermission('crm:obtain-guest:query')")
    public CommonResult<PageResult<ObtainGuestRespVO>> getObtainGuestPage(@Valid ObtainGuestPageReqVO pageReqVO) {
        PageResult<ObtainGuestRespVO> pageResult = obtainGuestService.getObtainGuestPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出订单获客主 Excel")
    @PreAuthorize("@ss.hasPermission('crm:obtain-guest:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportObtainGuestExcel(@Valid ObtainGuestPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ObtainGuestRespVO> list = obtainGuestService.getObtainGuestPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "订单获客主.xls", "数据", ObtainGuestRespVO.class,
                        BeanUtils.toBean(list, ObtainGuestRespVO.class));
    }


}